package com.client.ryt.activity;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Build;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.client.ryt.R;
import com.client.ryt.adapter.TradeRecordAdapter;
import com.client.ryt.adapter.VoucherRecordAdapter;
import com.client.ryt.basic.BaseActivity;
import com.client.ryt.basic.BaseException;
import com.client.ryt.basic.BaseLiteners;
import com.client.ryt.basic.BaseResponse;
import com.client.ryt.basic.BaseTask;
import com.client.ryt.config.LoginConfig;
import com.client.ryt.entity.biz.TradeRecordEntity;
import com.client.ryt.entity.biz.VoucherRecordEntity;
import com.client.ryt.entity.parameter.TradeRecordParameters;
import com.client.ryt.task.http.request.TradeRecordRequest;
import com.client.ryt.task.http.response.TradeRecordResponse;

import java.util.ArrayList;
import java.util.List;

/******
 * 交易记录
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class TradeRecordActivity extends BaseActivity {
    TradeRecordActivity oThis = this;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trade_record);
        this.initView();
    }

    private void initView(){
        // 提示getActionBar方法一定在setContentView后面
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
       // actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
        actionBar.addTab(actionBar.newTab().setText("交易记录").setTabListener(new MyTabListener<FragmentPage1>(this, FragmentPage1.class)));
        actionBar.addTab(actionBar.newTab().setText("充值记录").setTabListener(new MyTabListener<FragmentPage2>(this,FragmentPage2.class)));
    }

    public static class FragmentPage1 extends Fragment{

        ListView lv = null;
        TradeRecordAdapter tradeRecordAdapter;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view =  inflater.inflate(R.layout.fragment_traderecord_a, null);
            return view;
        }

        @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            lv = (ListView)view.findViewById(R.id.listview);
            TradeRecordParameters tradeRecordParameters = new TradeRecordParameters();
            tradeRecordParameters.setToken(LoginConfig.getLoginConfig().getToken());
            tradeRecordParameters.setUsername(LoginConfig.getLoginConfig().getShopMember().getUsername());
            tradeRecordParameters.setShopid(LoginConfig.getLoginConfig().getShopMember().getShop_id());
            tradeRecordParameters.setPagesize(5);
            tradeRecordParameters.setPageindex(1);
            new BaseTask<TradeRecordRequest,TradeRecordResponse>((BaseActivity)getActivity(),TradeRecordRequest.class,TradeRecordResponse.class,tradeRecordParameters,new BaseLiteners.BaseResponseListener() {
                @Override
                public void OnFail(BaseException e) {
                    ((BaseActivity) getActivity()).showToast(e.getErrmsg());
                }

                @Override
                public void OnSuccess(BaseResponse o) {
                    TradeRecordResponse tradeRecordResponse = (TradeRecordResponse)o;
                    tradeRecordAdapter = new TradeRecordAdapter(getActivity(),tradeRecordResponse.getList());
                    lv.setAdapter(tradeRecordAdapter);
                }
            },true).execute();
        }
    }

    public static class FragmentPage2 extends Fragment{

        ListView listview;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_traderecord_b, null);
        }

        @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            listview = (ListView)view.findViewById(R.id.listview);
            VoucherRecordAdapter voucherRecordAdapter = new VoucherRecordAdapter(this.getActivity(),getList());
            listview.setAdapter(voucherRecordAdapter);
        }

        private static List<VoucherRecordEntity> getList(){
            List<VoucherRecordEntity> list = new ArrayList<VoucherRecordEntity>();
            VoucherRecordEntity voucherRecordEntity = new VoucherRecordEntity();
            voucherRecordEntity.setRunningNumber("0001");
            voucherRecordEntity.setDate("2014-12-14");
            voucherRecordEntity.setScore("300");
            list.add(voucherRecordEntity);
            voucherRecordEntity = new VoucherRecordEntity();
            voucherRecordEntity.setRunningNumber("0002");
            voucherRecordEntity.setDate("2014-12-14");
            voucherRecordEntity.setScore("300");
            list.add(voucherRecordEntity);
            voucherRecordEntity = new VoucherRecordEntity();
            voucherRecordEntity.setRunningNumber("0003");
            voucherRecordEntity.setDate("2014-12-14");
            voucherRecordEntity.setScore("300");
            list.add(voucherRecordEntity);
            voucherRecordEntity = new VoucherRecordEntity();
            voucherRecordEntity.setRunningNumber("0004");
            voucherRecordEntity.setDate("2014-12-14");
            voucherRecordEntity.setScore("300");
            list.add(voucherRecordEntity);
            voucherRecordEntity = new VoucherRecordEntity();
            voucherRecordEntity.setRunningNumber("0005");
            voucherRecordEntity.setDate("2014-12-14");
            voucherRecordEntity.setScore("300");
            list.add(voucherRecordEntity);
            voucherRecordEntity = new VoucherRecordEntity();
            voucherRecordEntity.setRunningNumber("0006");
            voucherRecordEntity.setDate("2014-12-14");
            voucherRecordEntity.setScore("300");
            list.add(voucherRecordEntity);
            voucherRecordEntity = new VoucherRecordEntity();
            voucherRecordEntity.setRunningNumber("0008");
            voucherRecordEntity.setDate("2014-12-14");
            voucherRecordEntity.setScore("300");
            list.add(voucherRecordEntity);
            voucherRecordEntity = new VoucherRecordEntity();
            voucherRecordEntity.setRunningNumber("0022");
            voucherRecordEntity.setDate("2014-12-14");
            voucherRecordEntity.setScore("3420");
            list.add(voucherRecordEntity);
            voucherRecordEntity = new VoucherRecordEntity();
            voucherRecordEntity.setRunningNumber("0009");
            voucherRecordEntity.setDate("2014-12-14");
            voucherRecordEntity.setScore("400");
            list.add(voucherRecordEntity);
            return list;
        }

    }

    class MyTabListener<T extends Fragment> implements ActionBar.TabListener {
        private Fragment fragment;
        private final Activity mActivity;
        private final Class<T> mClass;
        public MyTabListener(Activity activity, Class<T> clz){
            mActivity = activity;
            mClass = clz;
        }
        @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
        @Override
        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
            if(fragment == null){
                fragment = Fragment.instantiate(mActivity, mClass.getName());
                ft.add(android.R.id.content, fragment, null);
            }
            ft.attach(fragment);
        }

        @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
        @Override
        public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
            if (fragment != null) {
                ft.detach(fragment);
            }
        }

        @Override
        public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.trade_record, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
